package com.lqzs.businessmanager.ui.main.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.lqzs.businessmanager.R;
import com.lqzs.businessmanager.bean.ReplyInfo;
import com.lqzs.businessmanager.bean.event.ServiceEvent;
import com.lqzs.businessmanager.bean.response.LoginShopResponse;
import com.lqzs.businessmanager.ui.adapter.DetailCommentAdapter;
import com.lqzs.businessmanager.utils.JavaCallKotlinUtil;
import com.lqzs.businessmanager.utils.ThreadPoolHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class DetailCommentActivity extends Activity {

    private ListView mListView;
    private TextView mTvConfirm;
    private DetailCommentAdapter mAdapter;
    public String platformName;
    public String address;
    private int branchId;

    private TextView to_start_reply;
    private TextView detail_title;
    private TextView detail_platformname;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_comment);
        LoginShopResponse vendorInfo = (LoginShopResponse) getIntent().getSerializableExtra("vendorInfo");
        if (vendorInfo != null) {
            platformName = vendorInfo.getPlatform_name();
            branchId = vendorInfo.getBranch_id();
            address = vendorInfo.getAddress();
        }
//        mTvConfirm = findViewById(R.id.tv_detail_confirm);
//        mTvConfirm.setOnClickListener(view -> open(vendorInfo));

        to_start_reply=findViewById(R.id.to_start_reply);
        detail_title=findViewById(R.id.detail_title);
        detail_platformname=findViewById(R.id.detail_platformname);

        detail_title.setText(platformName);
        detail_platformname.setText(address);

        to_start_reply.setOnClickListener(view -> open(vendorInfo));


        mListView = findViewById(R.id.lv_detail_comment);
        mAdapter = new DetailCommentAdapter(this);
        mListView.setAdapter(mAdapter);

        List<ReplyInfo> infos = ThreadPoolHelper.executeWaitTaskFinish(() -> {
            JavaCallKotlinUtil util = new JavaCallKotlinUtil();
            return util.callSuspendGetAll(platformName, branchId);
        });
        mAdapter.setItems(infos);
        findViewById(R.id.detail_log_out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DetailCommentActivity.this.finish();
            }
        });
    }


    private void open(LoginShopResponse vendorInfo) {
        ThreadPoolHelper.execute(() -> {
            JavaCallKotlinUtil util = new JavaCallKotlinUtil();
            List<ReplyInfo> items = mAdapter.getItems();
            for (ReplyInfo item : items) {
                util.callSuspendUpdateReplayContentById(item.getId(), item.getReplyContent());
            }
        });

        ServiceEvent event = new ServiceEvent();
        event.setVendorInfo(vendorInfo);
        EventBus.getDefault().post(event);
        finish();
    }



}
